<?php

class MLoaisanpham extends CI_Model {

    private $matl;
    private $tentl;

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->OnCreate();
    }

    function OnCreate() {
        $this->matl = '';
        $this->tentl = '';
    }

    public function set_matl($data) {
        $this->matl = $data;
    }

    public function set_tentl($data) {
        $this->tentl = $data;
    }

    public function get_matl() {
        return $this->matl();
    }

    public function get_tentl() {
        return $this->tentl;
    }

//////////////////////////
    public function Save() {
        $data = array("matl" => $this->matl, "tentl" => $this->tentl);
        $this->db->insert("loaisanpham", $data);
    }

    public function Insert($matl, $tentl) {
        $data = array("matl" => $matl, "tentl" => $tentl);
      return  $this->db->insert("loaisanpham", $data);
    }
    public function UpdateByID(){
         $arr["matl"] = $this->matl;
        $arr["tentl"] = $this->tentl;
        $this->db->where('matl',$this->matl);
        return $this->db->update('loaisanpham',$arr);
    }

    public function UpdateField($strWhere = NULL) {
        $arr = array();
        $arr["matl"] = $this->matl;
        $arr["tentl"] = $this->tentl;
        if (!empty($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
        }
        $this->db->update("loaisanpham", $arr);
    }

    public function Delete($strWhere = NULL) {
        if (!is_null($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
            $this->db->delete("loaisanpham");
        }
        else
            $this->db->empty_table('loaisanpham');
    }

    public function get_top_id() {
        $Query = $this->db->query("select * from loaisanpham");
        $dem = $Query->num_rows();
        return $dem + 1;
    }

    public function getAllLoaisp() {
        return $this->db->get('loaisanpham')->result_array();
    }

    public function DeleteByID($id) {
        return $this->db->where('matl', $id)->delete('loaisanpham');
    }

    public function SearchByIDorName($keyword) {
        return $this->db->like('matl', $keyword)->or_like('tentl', $keyword)->get('loaisanpham')->result_array();
    }
    public function getByID($matl) {
        return $this->db->get_where('loaisanpham', array('matl' => $matl))->row_array();
    }

}

?>
